<!-- application/views/scripts/level/add-level.phtml -->
<script type="text/javascript">
    $(document).ready(function() {
        $('#questionnaire option:first').attr('selected', 'selected');
        $('#level_id option[value=<?php echo $this->level_id ?>]').attr('selected', 'selected');
    });
    
    function addQuestionnaireToTest(t_id) {
        var q_id = $("select#questionnaire option:selected").val();
        var q_name = $("select#questionnaire option:selected").text();
        var url = BASE_URL + '/test/add-questionnaire/';
        var data = 'q_id='+q_id+'&t_id='+t_id;
        var row_temp = '<tr id="temp_row"><td colspan="2">Adding...</td></tr>';
        $(row_temp).appendTo($('#questions-table > tbody:last')).hide().fadeIn(500, function() {
                      $('#questions-table > tbody > tr#no-questionnaires').fadeOut(500);
                      $('#questions-table > tbody > tr#no-questionnaires').remove();
                      $('tr:even').addClass('odd');
                      $('tr:odd').addClass('even');
                  });
                  
        $.ajax({
          type: 'POST',
          url: url,
          data: data,
          success: function(data) {
              if(data=='ok') {

                  /*if ($('#questions-table > tbody:first').text().trim() == 'There is no questionnaires.') {
                      $('#questions-table > tbody:first').hide();
                  }*/

                  var row = '<tr id="q_'+q_id+'"><td>';
                  row+='<a href="'+BASE_URL+'/questionnaire/edit-questionnaire/?questionnaire_id='+q_id+'">'+q_name+'</a>';
                  row+='</td><td class="actions">';
                  row+='<a href="#questions-table" onclick="javascript:removeTestQuestionnaire(\''+BASE_URL+'/test/remove-questionnaire/?t_id='+t_id+'&amp;q_id='+q_id+'\', \''+q_id+'\');"><div class="ui-state-default ui-corner-all" title="Remove from this Test" style="float:left; margin: 3px 3px 0 0;"><span class="ui-icon ui-icon-trash"></span></div></a>';
                  row+='<a href="'+BASE_URL+'/questionnaire/edit-questionnaire/?questionnaire_id='+q_id+'"><div class="ui-state-default ui-corner-all" title="Edit" style="float:left; margin: 3px 3px 0 0;"><span class="ui-icon ui-icon-pencil"></span></div></a>';
                  row+='</td></tr>';

                  $(row).appendTo($('#questions-table > tbody:last')).hide().fadeIn(500, function() {
                      $('#questions-table > tbody > tr#no-questionnaires').fadeOut(500);
                      $('#questions-table > tbody > tr#no-questionnaires').remove();
                      $('tr:even').addClass('odd');
                      $('tr:odd').addClass('even');
                  });

              } else if (data=='fail') {
                  errorMessage('Error', 'There has been an error trying to add the questionnaire. Please try again in a few minutes.')
              } else if (data=='no-selected') {
                  errorMessage('Error', 'You must choose a questionnaire from the list.')
              } else if (data=='repeated') {
                  errorMessage('Repeated', 'You just can add a questionnaire one time to each test.');
              }

              $('#questions-table > tbody > tr#temp_row').fadeOut(500);
              $('#questions-table > tbody > tr#temp_row').remove();

              $('#questionnaire option:first').attr('selected', 'selected');
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
              alert("There was an error, please contact the administrator.");
          }
        });
    }

    function removeTestQuestionnaire(url, id) {
        // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
        $("#dialog").dialog("destroy");

        var aux_id = id;
        $("#dialog-remove-confirm").dialog({
            resizable: false,
            height:180,
            width: 400,
            modal: true,
            buttons: {
                'Delete': function() {

                   $.ajax({
                      type: 'GET',
                      url: url,
                      success: function(data) {
                          if(data=='ok') {
                              $('#questions-table > tbody > tr#q_'+aux_id).fadeOut(1000, function() {
                                  $('#questions-table > tbody > tr:even').addClass('even');
                                  $('#questions-table > tbody > tr:odd').addClass('odd');
                                  $('#questions-table > tbody > tr#q_'+aux_id).remove();
                                  if(!$('#questions-table > tbody > tr').length) {
                                      $('<tr id="no-questionnaires"><td>There is no questionnaires.</td></tr>').appendTo($('#questions-table > tbody:last')).hide().fadeIn(500);
                                  }
                              });
                              
                          } else if (data=='fail') {
                              errorMessage('Error', 'There has been an error trying to add the questionnaire. Please try again in a few minutes.')
                          } else if (data=='no-selected') {
                              errorMessage('Error', 'You must choose a questionnaire from the list.')
                          }
                      }
                    });
                    $(this).dialog('destroy');
                },
                Cancel: function() {
                    $(this).dialog('destroy');
                }
            }
        });
    }

</script>

<div id="section-content">
<h2>Admin > Edit test</h2>
Please, complete the form below.

<?php
$this->form->setAction($this->url()."?test_id=".$this->test_id);
echo $this->form;

?>

<div id="dialog-remove-confirm" title="Are you sure?" style="display: none;">
    <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These Questionnaire will be removed from this Test.<br />Are you sure?
</div>

<h3>Questionnaires</h3>
<form name="add-questionnaire-to-test" id="id_add-questionnaire-to-test" method="post">
    <select name="questionnaire" id="questionnaire">
        <option value='-1'>-- Select a questionnaire to add --</option>
        <?php

        foreach ($this->allquestionnaires as $entry) {
          echo "<option value='".$entry->id."'>".$entry->name." ( ".$entry->getFailsNumber()." fails )</option>\n";
        }
        ?>
    </select>
    
    <a href="#questions-table" onclick="javascript:addQuestionnaireToTest('<?php echo $this->test_id ?>');"><div class="ui-state-default ui-corner-all" title="Add questionnaire" style="float:left; margin: 3px 3px 0 0;"><span class="ui-icon ui-icon-circle-arrow-s"></span></div></a>
</form>
<table class="general-table ui-widget ui-widget-content table" cellspacing="0" id="questions-table">
<thead>
    <tr class="ui-widget-header">
        <th colspan="2">Questionnaire</th>
    </tr>
</thead>
<tbody>


    <?php

    if (count($this->questionnaires) == 0) {
        ?>
        <tr id="no-questionnaires">
            <td>There is no questionnaires.</td>
        </tr>
    <?php
    }

    $class = "odd";
    foreach ($this->questionnaires as $entry):
        $class = ($class=='even') ? 'odd' : 'even';
    ?>
        <tr class="<?php echo $class ?>" id="q_<? echo $entry->id ?>">
            <td><a href="<? echo $this->baseUrl() ?>/questionnaire/edit-questionnaire/?questionnaire_id=<?php echo $this->escape($entry->id) ?>"><?php echo $this->escape($entry->name) ?> ( <?php echo $this->escape($entry->getFailsNumber()) ?> fails )</a></td>
            <td class="actions">
                <a href="#questions-table" onclick="javascript:removeTestQuestionnaire('<? echo $this->baseUrl() ?>/test/remove-questionnaire/?t_id=<?php echo $this->test_id ?>&q_id=<?php echo $this->escape($entry->id) ?>', '<?php echo $this->escape($entry->id) ?>');"><div class="ui-state-default ui-corner-all" title="Remove from this Test" style="float:left; margin: 3px 3px 0 0;"><span class="ui-icon ui-icon-trash"></span></div></a>
                <a href="<? echo $this->baseUrl() ?>/questionnaire/edit-questionnaire/?questionnaire_id=<?php echo $this->escape($entry->id) ?>"><div class="ui-state-default ui-corner-all" title="Edit" style="float:left; margin: 3px 3px 0 0;"><span class="ui-icon ui-icon-pencil"></span></div></a>
            </td>
        </tr>
    <?php  endforeach ?>

</tbody>
</table>

</div>